<template>
  <div class="warpper">
    <div class="toolbar" style="float: right;">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <!--        <el-form-item label="Category Name">
          <el-input v-model="searchForm.name" @keyup.enter.native="search" placeholder="Enter Name">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="find()" plain>Search</el-button> -->
        <el-button type="primary" v-permission="['customer_groups.store']" plain @click="groupadd"
          style="margin:0 30px;">+ New</el-button>
        <!-- </el-form-item> -->
      </el-form>
    </div>
    <div class="table">
      <el-table :data="tableData" size="small" stripe border style="width: 100%">

        <el-table-column type="selection" width="50" align="center" />

        <el-table-column prop="name" label="Group Name" min-width="150" align="center" sortable/>

        <el-table-column prop="groupcode" label="Group ID" align="center" sortable/>

        <el-table-column prop="startdate" label="Contract" style="width: 460px;" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.startdate}}~{{scope.row.enddate}}</span>
          </template>
        </el-table-column>

        <!-- <el-table-column prop="status" label="Status" min-width="150" align="center" /> -->

        <el-table-column prop="enddate" label="Status" min-width="150" align="center" sortable>
          <template v-slot="scope">
            <span v-if="scope.row.enddate && parseDateStr(scope.row.enddate)<mydate"
              style="color:#fb8503">Expired</span>
            <span v-else>Normal</span>
          </template>
        </el-table-column>

        <el-table-column prop="created_at" label="Created Time" min-width="200" align="center" sortable>
          <template v-slot="scope">
            <span>{{ scope.row.created_at | showDateTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="Actions" width="200">
          <template slot-scope="scope">
            <el-button plain v-permission="['customer_groups.show']" @click="edit(scope.row.id)">edit</el-button>
            <el-button plain v-permission="['customer_groups.destroy']" type="danger"
              @click="del(scope.row.id)">delete</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination v-if="isPaginate" background :current-page="page.current_page" :page-sizes="page.sizes"
        :page-size="page.per_page" layout="prev, pager, next" :total="page.total" @size-change="sizeChange"
        @current-change="currentChange" />
    </div>
  </div>
</template>

<script>
  import CURD from "@/mixin/CURD";
  import {
    index
  } from "@/api/customer_group";
  export default {
    mixins: [CURD],
    name: "customer_group_index",
    data() {
      return {
        mydate: '',
        module: "customer_group",
        isPaginate: true,
      };
    },
    async created() {
      const today = new Date();
      this.mydate = today
    },
    methods: {
      parseDateStr(dateStr) {
        const [month, day, year] = dateStr.split('-').map(Number);
        return new Date(year, month - 1, day);
      },
      groupadd() {
        this.$router.push({
          path: '/customergroup/groupadd',
          query: {
            id: null
          }
        })
      },
      edit(id) {
        this.$router.push({
          path: '/customergroup/editgroup',
          query: {
            id: id
          }
        })
      }
    }
  };
</script>

<style>
  .table,
  .toolbar,
  .page {
    margin: 20px;
  }

  .toolbar {
    margin-top: 10px;
    padding: 5px;
  }

  .table {
    margin-bottom: 10px;
  }

  .page {
    margin-top: 10px;
    text-align: center;
  }

  .avatar {
    width: 50%;
    height: 50%;
    display: block;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
</style>
